package com.kaili.basic.controlle;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BarcoderuleAddDto;
import com.kaili.basic.pojo.dto.BarcoderuleDto;
import com.kaili.basic.pojo.entity.BaseBarcodeRule;
import com.kaili.basic.service.IBaseBarcodeRuleService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "条码规则", description = "条码规则管理相关API")
public class BaseBarcodeRuleController {
    @Autowired
    IBaseBarcodeRuleService baseBarcodeRuleService;


    @GetMapping(value = "/barcoderules/check")
   @ApiOperation(value = "校验公司是否维护过规则", notes = "校验公司是否维护过规则")
   public Boolean maintainCount(){
       return  baseBarcodeRuleService.selectCount();
   }


    //添加条码规则
    @PostMapping(value = "/barcoderules")
    @ApiOperation(value = "添加流水号规则", notes = "添加流水号规则")
    public RestStatus addNewBaseBarcodeRule(@Validated @RequestBody BarcoderuleAddDto barcoderuleAddDto) {

        /*if (barcoderuleAddDto.getEwbType().longValue() == 70L) {
            Boolean  flag=baseBarcodeRuleService.selectCount();
            if(flag==false){
                return  CommonStatus.MAX_COUNT;
            }
        }*/
        if (baseBarcodeRuleService.ExitSerialNum(barcoderuleAddDto.getEwbType().longValue())){
            return  CommonStatus.MAX_COUNT;
        }


        boolean flag = baseBarcodeRuleService.addNewBaseBarcodeRule(barcoderuleAddDto);
        if (flag){
            return CommonStatus.ADD_OK;
        }else {
            return CommonStatus.REPEAT;
        }

    }
    //修改条码规则
    @PutMapping(value = "/barcoderules")
    @ApiOperation(value = "修改条码规则(废弃)", notes = "修改条码规则(废弃)")
    public RestStatus updateBaseRole(@RequestBody BaseBarcodeRule baseBarcodeRule) {

        int flag = baseBarcodeRuleService.updateById(baseBarcodeRule);


        if (flag > 0 ){
            return CommonStatus.UPDATE_OK;
        }else {
            return CommonStatus.UPDATE_FAIL;
        }

    }


    //删除条码规则
    @DeleteMapping(value = "/barcoderules/bat")
    @ApiOperation(value = "删除流水号规则", notes = "删除流水号规则")
    public RestStatus deleteDep(@RequestParam Long[] ids) {
        List<Long> idss = new ArrayList<Long>();
        for (int i = 0; i < ids.length ;i++){
           BaseBarcodeRule baseBarcodeRule = baseBarcodeRuleService.getBaseBarcodeRuleById(ids[i]);
            BaseBarcodeRule zBarCodeRule = baseBarcodeRuleService.getBaseBarcodeRuleByParentId(baseBarcodeRule.getId().longValue());
           if (baseBarcodeRule != null){
               baseBarcodeRule.setRdstatus(0);
               baseBarcodeRuleService.updateById(baseBarcodeRule);
           }
           //删除对应的子单规则
           if (zBarCodeRule != null){
               zBarCodeRule.setRdstatus(0);
               baseBarcodeRuleService.updateById(zBarCodeRule);
           }

        }
        return CommonStatus.DELETE_OK;



    }

    //根据条件分页查询条码规则
    @GetMapping(value = "/barcoderules")
    @ApiOperation(value = "根据条件分页查询条码规则", notes = "根据条件分页查询条码规则")
    public Page<BaseBarcodeRule> queryPagedList(@ModelAttribute BarcoderuleDto barcoderuleDto) {
        BaseBarcodeRule baseBarcodeRule = new BaseBarcodeRule();
        BeanUtils.copyProperties(barcoderuleDto, baseBarcodeRule);
        Page<BaseBarcodeRule> base = baseBarcodeRuleService.queryPagedList(new Page<BaseBarcodeRule>(baseBarcodeRule.getPage(), baseBarcodeRule.getPageSize()), baseBarcodeRule);
        return base;
    }


    //根据主单规则查找对应的最大件数
    @GetMapping(value = "/barcoderules/maxPieceByRule")
    @ApiOperation(value = "根据主单规则查找对应的最大件数", notes = "根据主单规则查找对应的最大件数")
    public int getMaxPieceByRule(@RequestParam("barcodeRule") String barcodeRule) {
        try {
            barcodeRule=URLDecoder.decode(barcodeRule,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotEmpty(barcodeRule)) {
            //查询件数
            int maxPiece = baseBarcodeRuleService.getBaseBarcodeRuleByRule(barcodeRule);
            return maxPiece;
        }else{
            return -1;
        }
    }




}
